package com.jinlejia.bbs.ui.gambit;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.databinding.BbsFragmentGambitMainBinding;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.ui.fun.BBSContributeActivity;
import com.jinlejia.bbs.ui.fun.BBSSearchActivity;
import com.sevenheaven.segmentcontrol.SegmentControl;

import java.util.ArrayList;
import java.util.List;

/**
 * 话题
 */
public class BBSGambitMainFragment extends Fragment implements SearchHelper{

    private BbsFragmentGambitMainBinding binding;
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view==null){
            view =inflater.inflate(R.layout.bbs_fragment_gambit_main, container, false);
            binding = DataBindingUtil.bind(view);
            initView();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        binding.setEvent(this);
        binding.setCommonEvent(CommonEvent.obtain());
        selectFragment(segmentIndex);
        return view;
    }
    private int segmentIndex;
    private void initView() {
        if (fragments==null){
            fragments = new ArrayList<>();
            BBSGambitIndexFragment indexFragment = new BBSGambitIndexFragment();
            BBSGambitAttentionFragment attentionFragment = new BBSGambitAttentionFragment();
            fragments.add(indexFragment);
            fragments.add(attentionFragment);
        }

        if (adapter==null){
            adapter = new FragmentAdapter(getFragmentManager(),fragments);
            binding.viewPager.setAdapter(adapter);
            binding.viewPager.setCurrentItem(0);
            binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    binding.segmentControl.setSelectedIndex(position);
                    segmentIndex = position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }



        binding.segmentControl.setOnSegmentControlClickListener(new SegmentControl.OnSegmentControlClickListener() {
            @Override
            public void onSegmentControlClick(int index) {
                segmentIndex = index;
                selectFragment(index);
            }
        });
    }


    private class FragmentAdapter extends FragmentPagerAdapter {

        private List<Fragment> mList = new ArrayList<>();
        FragmentAdapter(FragmentManager fm,List<Fragment> list) {
            super(fm);
            this.mList = list;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }
    }

    private FragmentAdapter adapter;
    private List<Fragment> fragments;
    private void selectFragment(int index){
        binding.viewPager.setCurrentItem(index);
    }

    //贡献榜
    public void toContributeActivity(View ignore){
        Intent intent = new Intent();
        intent.setClass(getContext(), BBSContributeActivity.class);
        startActivity(intent);
    }

    @Override
    public void search() {
        Intent intent = new Intent();
        intent.setClass(getContext(), BBSSearchActivity.class);
        startActivityForResult(intent,100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==100 && resultCode == Activity.RESULT_OK){
            binding.segmentControl.setSelectedIndex(0);
            segmentIndex = 0;
            selectFragment(0);
            final String name = data.getStringExtra("name");
            binding.segmentControl.post(new Runnable() {
                @Override
                public void run() {
                    List<Fragment> list = getFragmentManager().getFragments();
                    for (Fragment fragment:list) {
                        if (fragment instanceof SearchHelper2){
                            SearchHelper2 helper2 = (SearchHelper2) fragment;
                            helper2.searchInit(name);
                            break;
                        }
                    }
                }
            });
        }
    }
}
